package su.jiedai.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import net.kuakao.core.dto.PagerDTO;

import org.apache.commons.lang3.StringUtils;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import su.base.Constants;
import su.base.dao.SuBaseDAO;
import su.jiedai.dao.IJiedaiDAO;
import su.jiedai.entity.JiedaiEntity;


@Repository
public class JiedaiDAO extends SuBaseDAO implements IJiedaiDAO {
	
	
	public List<JiedaiEntity> queryByPage(PagerDTO s) throws SQLException {
		String sql = "select * from su_jiedai where jd_status=1";
		
		sql += " ORDER BY jd_date";
		
		return super.queryByPage(sql, new RowMapper<JiedaiEntity>() {

			public JiedaiEntity mapRow(ResultSet rs, int arg1) throws SQLException {
				JiedaiEntity entity = new JiedaiEntity();
				entity.setId(rs.getInt("id"));
				entity.setJdDate(rs.getDate("jd_date"));
				entity.setJdKey(rs.getString("jd_key"));
				entity.setJdDesc(rs.getString("jd_desc"));
				entity.setJdCode(rs.getString("jd_code"));
				entity.setJdType(rs.getInt("jd_type"));
				entity.setJdAmountJie(rs.getDouble("jd_amount_jie"));
				entity.setJdAmountDai(rs.getDouble("jd_amount_dai"));
				entity.setJdAmountJiezhuan(rs.getDouble("jd_amount_jiezhuan"));
				entity.setJdCreateTime(rs.getDate("jd_create_time"));
				entity.setJdStatus(rs.getInt("jd_status"));
				return entity;
			}
			
		}, s);
	}
	
	
	public List<JiedaiEntity> getAll(String year) throws SQLException {
		String sql = "select * from su_jiedai where jd_status=1";
		
		if(StringUtils.isNotBlank(year)){
			sql += " and jd_date > '"+year+"-1-1' and jd_date < '"+(year+1)+"-1-1'";
		}
		
		return super.query(sql + " order by jd_date", new RowMapper<JiedaiEntity>() {

			public JiedaiEntity mapRow(ResultSet rs, int arg1) throws SQLException {
				JiedaiEntity entity = new JiedaiEntity();
				entity.setId(rs.getInt("id"));
				entity.setJdDate(rs.getDate("jd_date"));
				entity.setJdKey(rs.getString("jd_key"));
				entity.setJdDesc(rs.getString("jd_desc"));
				entity.setJdCode(rs.getString("jd_code"));
				entity.setJdType(rs.getInt("jd_type"));
				entity.setJdAmountJie(rs.getDouble("jd_amount_jie"));
				entity.setJdAmountDai(rs.getDouble("jd_amount_dai"));
				entity.setJdAmountJiezhuan(rs.getDouble("jd_amount_jiezhuan"));
				entity.setJdCreateTime(rs.getDate("jd_create_time"));
				entity.setJdStatus(rs.getInt("jd_status"));
				return entity;
			}
			
		});
	}


	public JiedaiEntity getLatestJiedai() throws SQLException {
		String sql = "select * from su_jiedai where jd_status=1 order by id desc limit 1";
		return super.queryForObject(sql, new RowMapper<JiedaiEntity>() {
			public JiedaiEntity mapRow(ResultSet rs, int value) throws SQLException {
				JiedaiEntity entity = new JiedaiEntity();
				entity.setId(rs.getInt("id"));
				entity.setJdDate(rs.getDate("jd_date"));
				entity.setJdKey(rs.getString("jd_key"));
				entity.setJdDesc(rs.getString("jd_desc"));
				entity.setJdCode(rs.getString("jd_code"));
				entity.setJdType(rs.getInt("jd_type"));
				entity.setJdAmountJie(rs.getDouble("jd_amount_jie"));
				entity.setJdAmountDai(rs.getDouble("jd_amount_dai"));
				entity.setJdAmountJiezhuan(rs.getDouble("jd_amount_jiezhuan"));
				entity.setJdCreateTime(rs.getDate("jd_create_time"));
				entity.setJdStatus(rs.getInt("jd_status"));
				return entity;
			}
		});
	}
	
	public JiedaiEntity getById(int id) throws SQLException {
		String sql = "select * from su_jiedai where jd_status=1 and id=? limit 1";
		return super.queryForObject(sql, new Object[]{id}, new RowMapper<JiedaiEntity>() {
			public JiedaiEntity mapRow(ResultSet rs, int value) throws SQLException {
				JiedaiEntity entity = new JiedaiEntity();
				entity.setId(rs.getInt("id"));
				entity.setJdDate(rs.getDate("jd_date"));
				entity.setJdKey(rs.getString("jd_key"));
				entity.setJdDesc(rs.getString("jd_desc"));
				entity.setJdCode(rs.getString("jd_code"));
				entity.setJdType(rs.getInt("jd_type"));
				entity.setJdAmountJie(rs.getDouble("jd_amount_jie"));
				entity.setJdAmountDai(rs.getDouble("jd_amount_dai"));
				entity.setJdAmountJiezhuan(rs.getDouble("jd_amount_jiezhuan"));
				entity.setJdCreateTime(rs.getDate("jd_create_time"));
				entity.setJdStatus(rs.getInt("jd_status"));
				return entity;
			}
		});
	}

	
	public void remove(int id) throws SQLException {
		final String sql = "update su_jiedai set jd_status=? where id=?";
		super.update(sql, new Object[]{Constants.STATUS_DISABLE,id});
	}
	
	
	
	public void insert(JiedaiEntity o) throws SQLException {
		String sql = "insert into su_jiedai(jd_date ,jd_key ,jd_desc ,jd_code ,jd_type ,jd_amount_jie ,jd_amount_dai ,jd_amount_jiezhuan,jd_create_time ,jd_status ) values(? ,? ,? ,? ,? ,? ,? ,? ,?,? )";
		super.update(sql, new Object[]{o.getJdDate() ,o.getJdKey() ,o.getJdDesc() ,o.getJdCode() ,o.getJdType() ,o.getJdAmountJie() ,o.getJdAmountDai(),o.getJdAmountJiezhuan() ,o.getJdCreateTime() ,o.getJdStatus() });
	}
	
	
	
	public void update(JiedaiEntity jiedaiEntity) throws SQLException {
		StringBuffer updateSql = new StringBuffer("update su_jiedai set ");
		List<Object> args = new ArrayList<Object>();
		args.add(jiedaiEntity.getJdDate());
		updateSql.append("jd_date=?").append(",");
		args.add(jiedaiEntity.getJdKey());
		updateSql.append("jd_key=?").append(",");
		args.add(jiedaiEntity.getJdDesc());
		updateSql.append("jd_desc=?").append(",");
		args.add(jiedaiEntity.getJdCode());
		updateSql.append("jd_code=?").append(",");
		//args.add(jiedaiEntity.getJdType());
		//updateSql.append("jd_type=?").append(",");
		args.add(jiedaiEntity.getJdAmountJie());
		updateSql.append("jd_amount_jie=?").append(",");
		args.add(jiedaiEntity.getJdAmountDai());
		updateSql.append("jd_amount_dai=?").append(",");
		args.add(jiedaiEntity.getJdAmountJiezhuan());
		updateSql.append("jd_amount_jiezhuan=?").append(",");
			
		
		args.add(jiedaiEntity.getId());
			
		
		String sql = updateSql.substring(0, updateSql.length()-1) + " where id = ? "; 
		super.update(sql, args.toArray());
	}


	public void removeAll() throws SQLException {
		final String sql = "update su_jiedai set jd_status=? where 1=1";
		Object[] params = new Object[]{Constants.STATUS_DISABLE};
		super.update(sql, params);
		//log
		super.insertDaoLog(JiedaiDAO.class.getName(),sql,params,null);
	}
}

